package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyFeedbackAddBo;
import com.ruoyi.system.bo.ZsyyFeedbackQueryBo;
import com.ruoyi.system.bo.ZsyyFeedbackEditBo;
import com.ruoyi.system.domain.ZsyyFeedback;
import com.ruoyi.system.mapper.ZsyyFeedbackMapper;
import com.ruoyi.system.vo.ZsyyFeedbackVo;
import com.ruoyi.system.service.IZsyyFeedbackService;
import com.ruoyi.system.mapper.ZsyyFeedbackMapper;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 意见反馈Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyFeedbackServiceImpl extends ServicePlusImpl<ZsyyFeedbackMapper, ZsyyFeedback> implements IZsyyFeedbackService {
    @Autowired
    private ZsyyFeedbackMapper iZsyyFeedbackMapper;

    @Override
    public ZsyyFeedbackVo queryById(Long id){
        return iZsyyFeedbackMapper.selectInfo(id);
    }

    @Override
    public TableDataInfo<ZsyyFeedbackVo> queryPageList(ZsyyFeedbackQueryBo bo) {
		Page<ZsyyFeedbackVo>  page = iZsyyFeedbackMapper.queryPageList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }

    @Override
    public List<ZsyyFeedbackVo> queryList(ZsyyFeedbackQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyFeedbackVo.class);
    }

    private LambdaQueryWrapper<ZsyyFeedback> buildQueryWrapper(ZsyyFeedbackQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyFeedback> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getPhone()), ZsyyFeedback::getPhone, bo.getPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getTitle()), ZsyyFeedback::getTitle, bo.getTitle());
        lqw.eq(bo.getStatus() != null, ZsyyFeedback::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyFeedbackAddBo bo) {
        ZsyyFeedback add = BeanUtil.toBean(bo, ZsyyFeedback.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyFeedbackEditBo bo) {
    	bo.setUpdatetime(new Date());
        ZsyyFeedback update = BeanUtil.toBean(bo, ZsyyFeedback.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyFeedback entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
